import request from '@/utils/request';
import { ResponseData, SUCCESS_CODE } from '@/utils/types.d';

export async function queryAbnormalOrderList(params: any, sort: any) {
  const res: ResponseData<{
    total: number;
    data: AbnormalOrderTableItem[];
  }> = await request('/api/order/v1.0.0/abnormal/list', {
    method: 'GET',
    params: {
      data: params,
      sort,
    },
  });

  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res?.data.total, data: res.data.data };
  return { success: false, total: 0, data: [] };
}

/**
 * 获取临近异常记录
 * @param params 查询参数
 * @returns 异常记录
 */
export async function getNeiborhoodReocrd(params: QueryNeiborRecordParam) {
  if (!params.id) return { success: false, data: null };
  const res: ResponseData<AbnormalOrderTableItem> = await request(
    '/api/order/v1.0.0/abnormal/neibor',
    { params },
  );
  if (res?.code === SUCCESS_CODE) return { success: true, data: res.data };
  return { success: false, data: null };
}

/**
 * 获取上一条异常记录
 * @param id 订单id
 * @returns 异常记录
 */
export async function getLastAbnormalRecord(id: string) {
  if (!id) return { success: false, data: null };
  const res: ResponseData<AbnormalOrderTableItem> = await request(
    `/api/order/v1.0.0/abnormal/lastabo/${id}`,
  );
  if (res?.code === SUCCESS_CODE) return { success: true, data: res.data };
  return { success: false, data: null };
}

/**
 * 审核异常订单
 * @param params 审核参数
 * @returns 审核完成后的异常订单
 */
export async function reviseAbnormalOrder(params: AboCorrectParams) {
  const res: ResponseData<AbnormalOrderTableItem> = await request(
    '/api/order/v1.0.0/abnormal/revise',
    { method: 'POST', data: params },
  );
  if (res?.code === SUCCESS_CODE) {
    return { success: true, data: res.data, message: '审核成功' };
  }
  return { success: false, data: res?.data, message: res?.message || '审核失败' };
}

/**
 * 获取本次巡检的图片
 * @param taskId 巡检记录的taskId
 * @returns 图片对象
 */
export async function getPatrolImgs(taskId: string) {
  const res: ResponseData<PatrolImgParams> = await request('/api/patrol-record/v1.0.0/imgs', {
    params: { taskId },
  });
  if (res?.code === SUCCESS_CODE) {
    return { success: true, data: res.data, message: '获取图片成功' };
  }
  return { success: false, data: null, message: res?.message || '获取图片失败' };
}

/**
 * 获取泊位最近的三条停车记录
 * @param params 查询条件
 * @returns 停车记录数组
 */
export async function getHistoryParkingRecord(params: QueryHistoryRecordParam) {
  const res: ResponseData<SpotParkingRecordItem[]> = await request(
    '/api/parking-record/v1.0.0/spot/records',
    {
      params,
    },
  );
  if (res?.code === SUCCESS_CODE) {
    return { success: true, data: res.data, message: '获取历史记录成功' };
  }
  return { success: false, data: [], message: res?.message || '获取历史记录失败' };
}

/**
 * 预检车牌是否可用
 * @param params
 * @returns
 */
export async function precheckPlateNumber(params: PrecheckPlateParams) {
  if (!params.plateNumber || params.plateNumber == '无' || params.plateNumber == '未知车牌') {
    return { success: true, data: null, message: '' };
  }
  const res: ResponseData<PrecheckResultType> = await request(
    '/api/order/v1.0.0/abnormal/precheck',
    {
      method: 'POST',
      data: params,
    },
  );
  if (res?.code === SUCCESS_CODE) {
    return { success: true, data: null, message: '' };
  }
  return { success: false, data: res?.data || null, message: res?.message || '预检失败' };
}

/**
 * 获取当前异常订单的停车记录
 * @param recordId 停车记录id
 * @returns
 */
export async function getCurrRecord(recordId: string) {
  if (!recordId) return { success: false, data: null, message: '缺少停车记录id' };
  const res: ResponseData<ParkingRecordParams> = await request(
    '/api/parking-record/v1.0.0/abo/record',
    { params: { recordId } },
  );
  if (res?.code === SUCCESS_CODE) {
    return { success: true, data: res.data, message: '获取当前停车记录成功' };
  }
  return { success: false, data: null, message: res?.message || '获取当前停车记录失败' };
}

/**
 * 获取冲突订单
 * @param orderNo 订单号
 * @returns 冲突订单
 */
export async function getConflictOrder(orderNo: string) {
  if (!orderNo) return { success: false, data: null, message: '获取冲突订单失败' };
  const res: ResponseData<ConflictOrderParams> = await request(
    '/api/order/v1.0.0/abnormal/conflict',
    { params: { orderNo } },
  );
  if (res?.code === SUCCESS_CODE) {
    return { success: true, data: res.data, message: '获取冲突订单成功' };
  }
  return { success: false, data: null, message: res?.message || '获取冲突订单失败' };
}
